;; spinning-bears-starter.rkt

(require 2htdp/image)
(require 2htdp/universe)

; PROBLEM:
; 
; In this problem you will design another world program. In this program the changing 
; information will be more complex - your type definitions will involve arbitrary 
; sized data as well as the reference rule and compound data. But by doing your 
; design in two phases you will be able to manage this complexity. As a whole, this problem 
; will represent an excellent summary of the material covered so far in the course, and world 
; programs in particular.
; 
; This world is about spinning bears. The world will start with an empty screen. Clicking
; anywhere on the screen will cause a bear to appear at that spot. The bear starts out upright,
; but then rotates counterclockwise at a constant speed. Each time the mouse is clicked on the 
; screen, a new upright bear appears and starts spinning.
; 
; So each bear has its own x and y position, as well as its angle of rotation. And there are an
; arbitrary amount of bears.
; 
; To start, design a world that has only one spinning bear. Initially, the world will start
; with one bear spinning in the center at the screen. Clicking the mouse at a spot on the
; world will replace the old bear with a new bear at the new spot. You can do this part 
; with only material up through compound. 
; 
; Once this is working you should expand the program to include an arbitrary number of bears.
; 
; Here is an image of a bear for you to use: .


;; My world program  (make this more specific)

;; =================
;; Constants:
(define WIDTH 600)
(define HEIGHT 800)
(define mts (empty-scene WIDTH HEIGHT ))
  
(define b_img .)
(define tSpeed 10)
 

;; =================
;; Data definitions:
  
(define-struct bear (x y angle))
;;bear is a (make-struct (bear Natural Natural Integer))
;;interp. A bear is has:
;;           x: x-cor pos
;;           y: y-cor pos
;;           angle: turn angle
  
(define b1 (make-bear  (/ WIDTH 2)(/ HEIGHT 2) 0 ))
(define b2 (make-bear  (/ WIDTH 2)(/ HEIGHT 2) 50 ))



  
  

;; =================
;; Functions:

;; bear -> bear
;; start the world with (main (make-bear (/ WIDTH 2) (/ HEIGHT 2)))
;; 
(define (main bear)
  (big-bang bear                   ; bear
            (on-tick   tock)       ; bear -> bear
            (to-draw   render)   ; bear -> Image
            (on-mouse  new-bear)))      ; bear Integer Integer MouseEvent -> bear
            

;; bear -> bear
;; produce the next list of bears

(define (tock lob)(if (empty? lob) empty
                                   (cons (make-bear (bear-x (first lob)) (bear-y (first lob)) (+ (bear-angle (first lob)) tSpeed))
                                          (tock (rest lob)) ) ))



;; bear -> Image
;; render bear img 
(define (render lob) (if (empty? lob) mts (place-image (rotate (modulo(bear-angle (first lob)) 360) b_img) 
                                                       (bear-x (first lob)) (bear-y (first lob))
                                                         (render (rest lob) )) ))

;; mouse click -> bear
;; on click produces the next bear image

;;(main (cons (make-bear 300 400 0) (cons (make-bear 20 20 0) empty)))
(define (new-bear lob x y me)
  (cond [(mouse=? me "button-down") (if (empty? lob) (cons (make-bear x y 0) empty)
                                        (cons (first lob) (new-bear (rest lob) x y "button-down") ))]
        [else
         lob]))
;; MouseEvent